package com.littleken.zxing;

import com.google.zxing.BinaryBitmap;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatReader;
import com.google.zxing.Result;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.common.HybridBinarizer;

import javax.imageio.ImageIO;


import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;

/**
 * 本类用于解析二维码
 */
public class ReadQRCode_zxing {
    public static void main(String[] args) {

        try {
            //生成二维码解析对象
            MultiFormatReader formatReader  = new MultiFormatReader();
            //二维码存在路径
            File file = new File("D://img.png");
           //读取输入流
            BufferedImage image =ImageIO.read(file);
            //流转换
            BinaryBitmap binaryBitmap = new BinaryBitmap(new HybridBinarizer(new BufferedImageLuminanceSource(image)));
            //通过HasMap存放二维码参数
            HashMap hints = new HashMap();
            //二维码编码格式
            hints.put(EncodeHintType.CHARACTER_SET,"utf-8");
            //读取
            Result result = formatReader.decode(binaryBitmap,hints);
            //输出结果
            System.out.println(result.toString());
            System.out.println(result.getBarcodeFormat());
            System.out.println(result.getText());
        } catch (Exception e) {
            e.printStackTrace();
        }


    }
}
